package zpin.dao;

import org.hibernate.Query;

import util.dao.HibernateDaoImpl;
import zpin.model.Authorization;
import zpin.model.Person;

public class AuthorizationDaoImpl extends HibernateDaoImpl<Authorization, Long> implements AuthorizationDao{

	public Authorization match(Authorization authorization) {
		Query query = this.getSession().createQuery("from "+Authorization.class.getName()+" p where p.account='"+authorization.getAccount()+"' and p.password='"+authorization.getPassword()+"'");
		if(query.uniqueResult()!=null){
			return ((Authorization) query.list().get(0));
		}else{
			return null;
		}
	}
	
	public boolean matchPerson(Authorization authorization) {
		Query query = this.getSession().createSQLQuery("select * from authorization_role where authorization_id="+this.match(authorization).getId()+" and role_id=1");
		return query.uniqueResult()!=null;
	}
	
	public boolean matchCompany(Authorization authorization) {
		Query query = this.getSession().createSQLQuery("select * from authorization_role where authorization_id="+this.match(authorization).getId()+" and role_id=2");
		return query.uniqueResult()!=null;
	}
	
	public boolean matchAdmin(Authorization authorization) {
		Query query = this.getSession().createSQLQuery("select * from authorization_role where authorization_id="+this.match(authorization).getId()+" and role_id=3");
		return query.uniqueResult()!=null;
	}
	
	public boolean matchAccount(Authorization authorization){
		Query query = this.getSession().createQuery("from "+Authorization.class.getName()+" p where p.account='"+authorization.getAccount()+"'");
		return query.uniqueResult()!=null;
	}
}
